package org.c41.graphics.swing;

import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;

import org.c41.util.Check;

public class JBasicTable extends JPanel {

	protected JTable table;

	public JBasicTable(){
		this.table = new JTable();
		m_init();
	}

	public JBasicTable(JTable table){
		Check.notNull(table);
		this.table=table;
	}

	private void m_init(){
		setLayout(new GridLayout(1,1));
		add(new JScrollPane(table));
		table.getTableHeader().setReorderingAllowed(false);
	}

	public JTable getTable(){
		return this.table;
	}

	public void selectRows(int from, int to){
		table.addRowSelectionInterval(from, to);
	}

	public void selectColumns(int from, int to){
		table.addColumnSelectionInterval(from, to);
	}

	public void clearSelection(){
		table.clearSelection();
	}

	public boolean isCellSelected(int row, int column){
		return table.isCellSelected(row, column);
	}

	public boolean isRowSelected(int row){
		return table.isRowSelected(row);
	}

	public void selectAllRowsAndColumns(){
		table.selectAll();
	}

	public void setAutoResizeMode(AutoResizeMode mode){
		table.setAutoResizeMode(mode.code);
	}

	public AutoResizeMode getAutoResizeMode(){
		return AutoResizeMode.inner_getByCode(table.getAutoResizeMode());
	}

	public void setRowSelectionAllowed(boolean b){
		table.setRowSelectionAllowed(b);
	}

	public void setColumnSelectionAllowed(boolean b){
		table.setColumnSelectionAllowed(b);
	}

	public int getColumnCount(){
		return table.getColumnCount();
	}

	public int getRowCount(){
		return table.getRowCount();
	}

	public String getCoumnName(int index){
		return table.getColumnName(index);
	}

	public void setColumnMaxWidth(int column, int width){
		table.getColumnModel().getColumn(column).setMaxWidth(width);
	}

	public void setColumnMinWidth(int column, int width){
		table.getColumnModel().getColumn(column).setMinWidth(width);
	}

	public void setColumnPreferredWidth(int column, int width){
		table.getColumnModel().getColumn(column).setPreferredWidth(width);
	}

	public void fixColumnWidth(int column, int width){
		setColumnMaxWidth(column, width);
		setColumnMinWidth(column, width);
		setColumnPreferredWidth(column, width);
	}

	public void setColumnResizable(int column, boolean b){
		table.getColumnModel().getColumn(column).setResizable(b);
	}

	public void setRowHeight(int row, int height){
		Check.between(row,0,getRowCount()-1);
		table.setRowHeight(row,height);
	}

	public void setRowHeight(int height){
		table.setRowHeight(height);
	}

	public void setRowMargin(int margin){
		table.setRowMargin(margin);
	}

	public void addListSelectionListener(ListSelectionListener l){
		table.getSelectionModel().addListSelectionListener(l);
	}

	public int getRowAtPoint(int x, int y){
		return table.rowAtPoint(new Point(x,y));
	}

	public int getRowAtPoint(Point point){
		return table.rowAtPoint(point);
	}

	public int getColumnAtPoint(int x, int y){
		return table.columnAtPoint(new Point(x,y));
	}

	public int getColumnAtPoint(Point point){
		return table.columnAtPoint(point);
	}

	@Override
	public void addMouseListener(MouseListener l){
		table.addMouseListener(l);
	}

	public void setSelectionMode(SelectionMode mode){
		table.getSelectionModel().setSelectionMode(mode.code);
	}

	public SelectionMode getSelectionMode(){
		return SelectionMode.inner_getByCode(table.getSelectionModel().getSelectionMode());
	}

	public void setFillsViewportHeight(boolean b){
		table.setFillsViewportHeight(b);
	}
	
	public static enum SelectionMode{

		Multiple(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION),
		Single(ListSelectionModel.SINGLE_SELECTION),
		Interval(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

		final int code;

		private SelectionMode(int code){
			this.code=code;
		}

		static SelectionMode inner_getByCode(int code) {
			for(SelectionMode mode: SelectionMode.values()){
				if(mode.code==code){
					return mode;
				}
			}
			return null;
		}

	}
	
	public static enum AutoResizeMode{

		Off(JTable.AUTO_RESIZE_OFF),
		LastColumn(JTable.AUTO_RESIZE_LAST_COLUMN),
		NextColumn(JTable.AUTO_RESIZE_NEXT_COLUMN),
		AllColumns(JTable.AUTO_RESIZE_ALL_COLUMNS),
		SubsequentColumns(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);

		final int code;

		private AutoResizeMode(int code){
			this.code=code;
		}

		static AutoResizeMode inner_getByCode(int code) {
			for(AutoResizeMode mode: AutoResizeMode.values()){
				if(mode.code==code){
					return mode;
				}
			}
			return null;
		}

	}

}
